package com.ccs.transmittal;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import com.ccs.testcase.*;

public class Admin_Create_Transmittal_List {
	
	public double inpatient_amount, outpatient_amount,inpatient_pf, final_transmittal_amount,outpatient_pf;
	public static String inVisitNo[], outVisitNo[];
	static
	Misc_Transmittal mt = new Misc_Transmittal();
	Admin_Transmittal_List atl = new Admin_Transmittal_List();
	
	public void goto_create_transmittal_page(WebDriver driver){
		driver.findElement(By.xpath("//button[contains(., 'Create Transmittal')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public String search_and_generate_soa_ci(WebDriver driver, String hmo, String date){
		
		double num =  Math.floor((Math.random()*999)+1);
		String code = String.valueOf(num);
		code = code.replaceAll("\\.","");
		String finalCode = "trans"+code;
		
		driver.findElement(By.xpath("//input[@id='selHMO']")).sendKeys(hmo);
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.MINUTES);
		
		//Select list of HMO
		Actions action = new Actions(driver);
		WebElement mouseover = driver.findElement(By.xpath("//html/body/ul/li/a"));
        action.moveToElement(mouseover);
        action.click().perform();
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		
        Select loc = new Select(driver.findElement(By.xpath("//select[@id='patientType']")));
		loc.selectByVisibleText("ALL");
		driver.findElement(By.xpath("//input[@id='txtTransNo']")).sendKeys(finalCode);
		driver.findElement(By.xpath("//input[@id='txtTransDate']")).click();
		atl.manual_date_picker(driver, date);
		
		driver.findElement(By.xpath("//button[@id='btnSearchTrans']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		return finalCode;
	}
	public void verify_search_and_generate_soa_ci_table(WebDriver driver, String type, String hmo){
		
		//TODO
		String inVisitNumber[] = {  "5406000290", "5406000291" };
		
		String outVisitNumber[] = {
				"5406000299","406108526","406108527","406108528","406108530","5406000292",
				"406108525","406108529","5406000293","5406000296","5406000304",
				"5406000295","5406000297","5406000302","5406000298",
				"5406000300","5406000294","5406000301",
				"5406000303","5406000315",
				"406108531"
		};
		
		String cocoOutVisitNo[] = { "406108520", "5406000255", "5406000257" }; 
		
		if(hmo.equals("eastwest")){
			//LOOP FOR INPATIENT EQUALS 2
			int a = inVisitNumber.length + 1;
			for(int y=1; y<a; y++){
				String[] inpatient_details = mt.get_table_details_inpatient_hospital(driver, y, type);
				if(Arrays.asList(inVisitNumber).contains(inpatient_details[0])){
					System.out.println(inpatient_details[0] + " : Match Found on array of visit number");
				}else{
					System.out.println(inpatient_details[0] + " : Match Not Found on array of visit number");
				}
			}
			int b = outVisitNumber.length + 1;
			//LOOP FOR OUTPATIENT EQUALS 21
			for(int y=1; y<b; y++){
				String[] outpatient_details = mt.get_table_details_outpatient_hospital(driver, y, type);
				if(Arrays.asList(outVisitNumber).contains(outpatient_details[0])){
					System.out.println(outpatient_details[0] + " : Match Found on array of visit number");
				}else{
					System.out.println(outpatient_details[0] + " : Match Not Found on array of visit number");
				}
			}	
		}else if(hmo.equals("cocolife")){
			String[] inpatient_details = mt.get_table_details_inpatient_hospital(driver, 1, type);
			
			if(inpatient_details[0].contains("5406000254")){
				System.out.println(inpatient_details[0] + " : Match Found on array of visit number");
			}else{
				System.out.println(inpatient_details[0] + " : Match Not Found on array of visit number");
			}
			int a = cocoOutVisitNo.length + 1;
			//LOOP FOR OUTPATIENT EQUALS 21
			for(int y=1; y<a; y++){
				String[] outpatient_details = mt.get_table_details_outpatient_hospital(driver, y, type);
				if(Arrays.asList(outVisitNumber).contains(outpatient_details[0])){
					System.out.println(outpatient_details[0] + " : Match Found on array of visit number");
				}else{
					System.out.println(outpatient_details[0] + " : Match Not Found on array of visit number");
				}
			}
		}
		
	}
	public void verify_transmittal_table_count(WebDriver driver, String hmo){
		
		String inpatient_count = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/span")).getText();
		String outpatient_count = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/span")).getText();
		if(hmo.equals("eastwest")){
			if(inpatient_count.equals("2 record/s found")){
				System.out.println(inpatient_count +": Inpatient Count is CORRECT");
			}else{
				System.out.println(inpatient_count +": Inpatient Count is INCORRECT");
			}
			if(outpatient_count.equals("21 record/s found")){
				System.out.println(outpatient_count +": Outpatient Count is CORRECT");
			}else{
				System.out.println(outpatient_count +": Outpatient Count is INCORRECT");
			}
		}else if(hmo.equals("cocolife")){
			if(inpatient_count.equals("1 record/s found")){
				System.out.println(inpatient_count +": Inpatient Count is CORRECT");
			}else{
				System.out.println(inpatient_count +": Inpatient Count is INCORRECT");
			}
			if(outpatient_count.equals("3 record/s found")){
				System.out.println(outpatient_count +": Outpatient Count is CORRECT");
			}else{
				System.out.println(outpatient_count +": Outpatient Count is INCORRECT");
			}
		}
		
		
	}
	public void proceed_with_transmittal(WebDriver driver, String hmo, String trans_no){
		
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//button[contains(.,'Proceed')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		String message = driver.findElement(By.xpath("//span[@id='spanMessage']")).getText();
		if(hmo.equals("eastwest")){
			if(message.contains("23 patient") && message.contains(trans_no)){
				System.out.println("Correct Count of Patients & Transmittal Number in Transmittal");
			}else{
				System.out.println("Incorrect Count of Patients & Transmittal Number in Transmittal");
			}
		}else if(hmo.equals("cocolife")){
			if(message.contains("4 patient") && message.contains(trans_no)){
				System.out.println("Correct Count of Patients & Transmittal Number in Transmittal");
			}else{
				System.out.println("Incorrect Count of Patients & Transmittal Number in Transmittal");
			}
		}
		driver.findElement(By.xpath("//button[@id='yesBtnDialog']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void verify_transmittal_table_headers(WebDriver driver, boolean check){
		//Verify table headers
		//INPATIENT (Hospital Bill and PF) ; OUTPATIENT (Hospital Bill and PF)
		
		mt.verify_table_header_inpatient_hospital(driver);
		mt.verify_table_header_outpatient_hospital(driver);
		driver.findElement(By.xpath("//ul[@id='myTab']/li[2]/a/b")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		mt.verify_table_header_inpatient_professional_fee(driver);
		mt.verify_table_header_outpatient_professional_fee(driver);
		mt.verify_location_create_transmittal(driver);
		mt.verify_transmittal_date_of_create_transmittal(driver);

		driver.findElement(By.xpath("//ul[@id='myTab']/li[1]/a/b")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);	
		
				
	}
	public Double compute_transmittal_table_headers(WebDriver driver){
		double final_amount = 0;
		
		//LOOP FOR INPATIENT EQUALS 2
		for(int y=1; y<3; y++){
			String[] inpatient_details = mt.get_table_details_inpatient_hospital(driver, y, "draft");
			System.out.println(inpatient_details[5]);
			String myString = inpatient_details[5].replaceAll(",", "");
			double amount = Double.parseDouble(myString);
			final_amount = final_amount + amount;
		}
		inpatient_amount = final_amount;
		
		//LOOP FOR OUTPATIENT EQUALS 21
		final_amount = 0;
		for(int y=1; y<22; y++){
			String[] outpatient_details = mt.get_table_details_outpatient_hospital(driver, y, "draft");
			System.out.println(outpatient_details[4]);
			double amount = Double.parseDouble((outpatient_details[4]).replaceAll(",", ""));
			final_amount = final_amount + amount;
		}
		outpatient_amount = final_amount;
		
		//LOOP FOR INPATIENT PF EQUALS 2
		final_amount = 0;
		driver.findElement(By.xpath("//ul[@id='myTab']/li[2]/a/b")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		for(int y=1; y<3; y++){
			String[] inpatient_pf_details = mt.get_table_details_inpatient_pf(driver, y);
			System.out.println(inpatient_pf_details[6]);
			String myString = inpatient_pf_details[6].replaceAll(",", "");
			double amount = Double.parseDouble(myString);
			final_amount = final_amount + amount;
		}
		inpatient_pf = final_amount;
		
		
		final_transmittal_amount = inpatient_amount + outpatient_amount +inpatient_pf;
		System.out.println("Inpatient Amount: "+inpatient_amount);
		System.out.println("Outpatient Amount: "+outpatient_amount);
		System.out.println("Inpatient PF Amount: "+inpatient_pf);
		System.out.println("Transmittal Amount: "+final_transmittal_amount);
		return final_transmittal_amount;
	}
	public void click_save_as_draft(WebDriver driver, String number){
		String message = mt.save_as_draft(driver);
		String trans_no = Transmittal_Details.transmittal_number;
		if(message.contains(number) && message.contains(trans_no)){
			System.out.println("Correct Count of Patients & Transmittal Number in Transmittal");
		}else{
			System.out.println("Incorrect Count of Patients & Transmittal Number in Transmittal");
		}
		driver.findElement(By.xpath("//button[@id='yesBtnDialog']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void click_generate_transmittal(WebDriver driver, String number, String trans_no){
		String message = mt.generate_transmittal(driver);
		if(message.contains(number) && message.contains(trans_no)){
			System.out.println("Correct Count of Patients & Transmittal Number in Transmittal");
		}else{
			System.out.println("Incorrect Count of Patients & Transmittal Number in Transmittal");
		}
		driver.findElement(By.xpath("//button[@id='yesBtnDialog']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void create_and_verify_tables(WebDriver driver, boolean set){
		
		List<WebElement> inVisitNum = driver.findElements(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr"));
		int sizeIn = inVisitNum.size();
		List<WebElement> outVisitNum = driver.findElements(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr"));
		int sizeOut = outVisitNum.size();
		
		inVisitNo = new String[sizeIn];
		outVisitNo = new String[sizeOut];
		
		int origSizeIn=sizeIn;
		for(int x=0;x<origSizeIn;x++){
			if(x>0){
				sizeOut--;	
			}
			String vn= driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr["+sizeIn+"]/td[2]")).getText();
			inVisitNo[x]= vn;
		}
		System.out.println(Arrays.toString(inVisitNo));
		if(set==true){
			set_amounts_of_transmittal("inpatient_visit_no", 0, inVisitNo);
		}
		
		int origSizeOut=sizeOut;
		for(int x=0;x<origSizeOut;x++){
			if(x>0){
				sizeOut--;	
			}
			String vn= driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+sizeOut+"]/td[2]")).getText();
			outVisitNo[x]= vn;
		}
		System.out.println(Arrays.toString(outVisitNo));
		if(set==true){
			set_amounts_of_transmittal("outpatient_visit_no", 0, outVisitNo);
		}
	}
	public void search_verify_dynamic_table(WebDriver driver, String type){
		int a = inVisitNo.length +1;
		for(int y=1; y<a; y++){
			String[] inpatient_details = mt.get_table_details_inpatient_hospital(driver, y, type);
			if(Arrays.asList(inVisitNo).contains(inpatient_details[0])){
				System.out.println(inpatient_details[0] + " : Match Found on array of visit number");
			}else{
				System.out.println(inpatient_details[0] + " : Match Not Found on array of visit number");
			}
		}
		
		int b = outVisitNo.length +1;
		for(int y=1; y<b; y++){
			String[] outpatient_details = mt.get_table_details_outpatient_hospital(driver, y, type);
			if(Arrays.asList(outVisitNo).contains(outpatient_details[0])){
				System.out.println(outpatient_details[0] + " : Match Found on array of visit number");
			}else{
				System.out.println(outpatient_details[0] + " : Match Not Found on array of visit number");
			}
		}
	}
	public void save_transmittal(WebDriver driver, String option, String transmittalNo, String check){
		
		if(option.equals("draft")){
			driver.findElement(By.xpath("//button[contains(.,'Save as Draft')]")).click();
		}else{
			driver.findElement(By.xpath("//button[contains(.,'Generate Transmittal')]")).click();
		}
		
		String message = driver.findElement(By.xpath("//span[@id='spanMessage']")).getText();
		
		if(message.contains(check) && message.contains(transmittalNo)){
			System.out.println("CORRECT Count of Patients & Transmittal Number in Transmittal");
		}else{
			System.out.println("INCORRECT Count of Patients & Transmittal Number in Transmittal");
		}
		
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//button[@id='yesBtnDialog']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	
	}
	public void set_amounts_of_transmittal(String option, double amount, String[] arr){
		
		if(option.equals("inpatient_hospital")){
			Transmittal_ToWebApp.hb_inpatient_amount = amount;
		}
		else if(option.equals("outpatient_hospital")){
			Transmittal_ToWebApp.hb_outpatient_amount = amount;
		}
		else if(option.equals("inpatient_pf")){
			Transmittal_ToWebApp.pf_inpatient_amount = amount;
		}
		else if(option.equals("outpatient_pf")){
			Transmittal_ToWebApp.pf_outpatient_amount = amount;
		}
		else if(option.equals("inpatient_visit_no")){
			Transmittal_ToWebApp.inVisitNo = arr;
		}
		else if(option.equals("outpatient_visit_no")){
			Transmittal_ToWebApp.outVisitNo = arr;
		}
	}
	public Double compute_transmittal_table_headers_dynamic(WebDriver driver, boolean set){
		double final_amount = 0;
		
		//GET INPATIENT COUNT
		List<WebElement> inVisitNum = driver.findElements(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr"));
		int sizeIn = inVisitNum.size()+1;
		
		//LOOP FOR INPATIENT HOSPITAL BILL
		for(int y=1; y<sizeIn; y++){
			String[] inpatient_details = mt.get_table_details_inpatient_hospital(driver, y, "draft");
			System.out.println(inpatient_details[5]);
			String myString = inpatient_details[5].replaceAll(",", "");
			double amount = Double.parseDouble(myString);
			final_amount = final_amount + amount;
		}
		inpatient_amount = final_amount;
		if(set==true){
			set_amounts_of_transmittal("inpatient_hospital", inpatient_amount, null);	
		}
				
		//GET OUTPATIENT COUNT
		List<WebElement> outVisitNum = driver.findElements(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr"));
		int sizeOut = outVisitNum.size()+1;
		
		//LOOP FOR OUTPATIENT HOSPITAL BILL
		final_amount = 0;
		for(int y=1; y<sizeOut; y++){
			String[] outpatient_details = mt.get_table_details_outpatient_hospital(driver, y, "draft");
			System.out.println(outpatient_details[4]);
			double amount = Double.parseDouble((outpatient_details[4]).replaceAll(",", ""));
			final_amount = final_amount + amount;
		}
		outpatient_amount = final_amount;
		if(set==true){
			set_amounts_of_transmittal("outpatient_hospital", outpatient_amount, null);	
		}
		
		//GO TO PF TAB
		driver.findElement(By.xpath("//ul[@id='myTab']/li[2]/a/b")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		//GET INPATIENT PF COUNT with VALIDATION IF NULL
		List<WebElement> inPf = driver.findElements(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr"));
		int sizeInPf = inPf.size();
		if(sizeInPf==1){
			String text = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr[1]/td[1]")).getText();
			if(text.contains("Nothing")){
				sizeInPf=0;
			}else{
				sizeInPf=sizeInPf+1;
			}
		}else{
			sizeInPf=sizeInPf+1;
		}
		
		//LOOP FOR INPATIENT PF
		final_amount = 0;
		for(int y=1; y<sizeInPf; y++){
			String[] inpatient_pf_details = mt.get_table_details_inpatient_pf(driver, y);
			System.out.println(inpatient_pf_details[6]);
			String myString = inpatient_pf_details[6].replaceAll(",", "");
			double amount = Double.parseDouble(myString);
			final_amount = final_amount + amount;
		}
		inpatient_pf = final_amount;
		if(set==true){
			set_amounts_of_transmittal("inpatient_pf", inpatient_pf, null);	
		}

		//GET OUTPATIENT PF COUNT with VALIDATION IF NULL
		List<WebElement> outPf = driver.findElements(By.xpath("//div[@id='collapseTwos']/div/div/table/tbody/tr"));
		int sizeOutPf = outPf.size();
		if(sizeOutPf==1){
			String text = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr[1]/td[1]")).getText();
			if(text.contains("Nothing")){
				sizeOutPf=0;
			}else{
				sizeOutPf=sizeOutPf+1;
			}
		}else{
			sizeOutPf=sizeOutPf+1;
		}
		
		//LOOP FOR OUTPATIENT PF
		final_amount = 0;
		for(int y=1; y<sizeOutPf; y++){
			String[] inpatient_pf_details = mt.get_table_details_outpatient_pf(driver, y);
			System.out.println(inpatient_pf_details[5]);
			String myString = inpatient_pf_details[5].replaceAll(",", "");
			double amount = Double.parseDouble(myString);
			final_amount = final_amount + amount;
		}
		outpatient_pf = final_amount;
		if(set==true){
			set_amounts_of_transmittal("outpatient_pf", outpatient_pf, null);	
		}
		
		final_transmittal_amount = inpatient_amount + outpatient_amount +inpatient_pf+outpatient_pf;
		System.out.println("Inpatient Amount: "+inpatient_amount);
		System.out.println("Outpatient Amount: "+outpatient_amount);
		System.out.println("Inpatient PF Amount: "+inpatient_pf);
		System.out.println("Outpatient PF Amount: "+outpatient_pf);
		System.out.println("Transmittal Amount: "+final_transmittal_amount);
		return final_transmittal_amount;
	}
	public void verify_transmittal_within_the_day(WebDriver driver){
		String text = driver.findElement(By.xpath("//span[@id='spanAlertMessage']")).getText();
		if(text.contains("Availment Date of Transmittal should be earlier than Current Date.")){
			System.out.println("CORRECT. Cannot create Transmittal on Current Date");
		}else{
			System.out.println("INCORRECT. Unmatched Error Message or Can create Transmittal on Current Date");
		}
	}
}
